//
//  ViewController.m
//  SDUserDefaultsDemo
//
//  Created by bnqc on 2019/4/19.
//  Copyright © 2019年 Dong. All rights reserved.
//

#import "ViewController.h"
#import "SDUserDefaults.h"
#import "TestUserDefaults.h"
#import "User.h"
#import "UICKeyChainStore.h"

@interface ViewController ()

@end

@implementation ViewController
- (IBAction)plistBtnClick:(id)sender {
    NSArray *names = @[@"zhangsan",@"lisi",@"wangwu",@"mazi"];
    //路径,文件的后缀名必须是  .plist
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [docPath stringByAppendingPathComponent:@"name.plist"];
    [names writeToFile:path atomically:YES];
    
    //读取plist文件的路径
   //根据plist的数据类型进行解析
   //1.根目录是数组./字典
   NSArray *data = [NSArray arrayWithContentsOfFile:path];
   //字典
   NSDictionary *dicData = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"data=======%@,dicData==========%@",data,dicData);
}


- (IBAction)keyChainBtnClick:(id)sender {
    //从钥匙串取值
    UICKeyChainStore *keychainStore = [UICKeyChainStore keyChainStore];
    NSString *acount = [keychainStore stringForKey:@"acount"];
    NSString *password = [keychainStore stringForKey:@"password"];
    NSLog(@"acount%@====password%@",acount,password);
    //从钥匙串存值
    [keychainStore setString:acount forKey:@"acount"];//存储账号
    [keychainStore setString:password forKey:@"password"];//存储密码
 
}


- (IBAction)unArchiverBtnClick:(id)sender {
    // 解档
     
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [docPath stringByAppendingPathComponent:@"user.archiver"];
    // 这个方法是过期的，但是目前我没有找到可以代替的方法
    User *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"user's userName = %@, user's password = %@", user.userName, user.password);

     
}

- (IBAction)archiverBtnClick:(id)sender {
    // 归档
       User *user = [[User alloc] init];
       
       user.userName = @"linmingjun";
       user.password = @"5788";
       //沙盒ducument目录
         NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
         //完整的文件路径
         NSString *path = [docPath stringByAppendingPathComponent:@"user.archiver"];
         
         //将数据归档到path文件路径里面
    if (@available(iOS 11.0, *)) {
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user requiringSecureCoding:NO error:nil];
        BOOL success = [data writeToFile:path atomically:YES];
        if (success) {
            NSLog(@"归档成功");
        }else {
            NSLog(@"归档失败");
        }

    } else {
        // Fallback on earlier versions
    }
         
       
       
   
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)addUserInfoAction:(id)sender {
    
    /*****赋值并且保存数据*****/
    [SDUserDefaults standardUserDefaults].name = @"用户数据";
    TextModel *testModel = [[TextModel alloc] init];
    testModel.name = @"骚栋";
    testModel.age = @(15);
    testModel.location = @"北京";
    [SDUserDefaults standardUserDefaults].testModel = testModel;
    
    //不需要归档持久化的数据,只用于本次声明周期中.会有检测过程
//    [SDUserDefaults standardUserDefaults].testModel.unEncodePropertys = @[@"age",@"names"];
    
    [[SDUserDefaults standardUserDefaults] saveAllPropertyAction];
    
    /*****赋值并且保存数据*****/
    [TestUserDefaults standardUserDefaults].name = @"测试数据";
    TextModel *test = [[TextModel alloc] init];
    test.name = @"测试骚栋";
    test.age = @(18);
    test.location = @"测试北京";
    [TestUserDefaults standardUserDefaults].testModel = test;
    
    //不需要归档持久化的数据,只用于本次声明周期中.会有检测过程
    //    [SDUserDefaults standardUserDefaults].testModel.unEncodePropertys = @[@"age",@"names"];
    
    [[TestUserDefaults standardUserDefaults] saveAllPropertyAction];
    
    NSLog(@"新增成功");
    
}

- (IBAction)showUserInfoAction:(id)sender {
    
    /*****获取数据*****/
    NSLog(@"%@",[SDUserDefaults standardUserDefaults].name);
    NSLog(@"%@",[SDUserDefaults standardUserDefaults].testModel.name);
    NSLog(@"%@",[SDUserDefaults standardUserDefaults].testModel.age);
    NSLog(@"%@",[SDUserDefaults standardUserDefaults].testModel.location);
    
    
    NSLog(@"%@",[TestUserDefaults standardUserDefaults].name);
    NSLog(@"%@",[TestUserDefaults standardUserDefaults].testModel.name);
    NSLog(@"%@",[TestUserDefaults standardUserDefaults].testModel.age);
    NSLog(@"%@",[TestUserDefaults standardUserDefaults].testModel.location);
    
}

- (IBAction)deleteUserInfoAction:(id)sender {
    
    /*****删除数据*****/
    [[SDUserDefaults standardUserDefaults] deleteAllPropertyAction];
    
    [[TestUserDefaults standardUserDefaults] deleteAllPropertyAction];
    
     NSLog(@"删除成功");
}




@end
